import axios from 'axios'
//将axios封装为vue的插件
var myaxios = {};
myaxios.install = function(Vue){

// axios.defaults.baseURL = 'http://pcprograme.tijian121.com';

//给所有的axios请求设置请求头
  axios.interceptors.request.use(function (config) {
    config.headers['Content-Type'] = 'application/json;charset=UTF-8';
    //得到请求地址
    if(config.url !== 'login'){
      config.headers.token=window.sessionStorage.getItem('token')
    }
    return config;
  });
  axios.interceptors.response.use(
    response => {
      if (response.data.errorCode == 300) {
        location.href = '/';
        return
      }else{
        return response;
      }
    },
    error => {
      return Promise.reject(error.response)   // 返回接口返回的错误信息
    }
  );
  //将axios作为实例方法添加vue中
  //$http一个约定
  Vue.prototype.$http = axios;
}

export default myaxios
